
from employment import crud
from database import SessionLocal
from fastapi import Depends, HTTPException, APIRouter
from sqlalchemy.orm import Session
import schema
from pydantic import BaseModel
from typing import Optional


from schema import Stu_employment


class EmploymentSearch(BaseModel):
    stu_id: Optional[int] = None
    cp_name: Optional[str] = None
    min_sal: Optional[int] = None
    max_sal: Optional[int] = None

emp_api = APIRouter()

def get_db():
    db = SessionLocal()
    try:
         yield db
    finally:
        db.close()

# 查询所有学生的就业情况
@emp_api.get('/employment/all',summary="所有学生的就业情况")
def show_employment_all(db: Session = Depends(get_db)):
    return crud.show_employment_info(db)

# 按照学生编号，公司名字，工资范围查询学生就业信息
@emp_api.post('/employment/search',summary="按照学生编号，公司名字，工资范围查询学生就业信息")
def get_employment_by(search_params: EmploymentSearch, db: Session = Depends(get_db)):
    return crud.get_employment_info_by(
        stu_id=search_params.stu_id, 
        cp_name=search_params.cp_name, 
        sal_min=search_params.min_sal, 
        sal_max=search_params.max_sal, 
        db=db
    )

# 更新学生就业信息
@emp_api.post('/employment/update',summary="更新学生就业信息")
def update_employment(stu_employ:Stu_employment,db: Session = Depends(get_db)):
    return crud.update_stu_employment(stu_emp=stu_employ,db=db)

# 删除学生就业信息
@emp_api.post('/employment/delete',summary="根据学生id删除就业信息")
def delete_employment(stu_id:int,db: Session = Depends(get_db)):
    return crud.delete_stu_employment(stu_id=stu_id,db=db)

# 添加学生就业信息
@emp_api.post("/employment/add",summary="添加学生就业信息")
def add_employment_info(stu_emp:Stu_employment,db: Session = Depends(get_db)):
    return crud.add_stu_employment(stu_emp=stu_emp,db=db)
